//
//  ChatMessageBean.swift
//  test-ios
//
//  Created by wyz5204 on 2023/7/26.
//


import Foundation

// MARK: - ChatMessageBean
class ChatMessageBean: Codable {
    var id: String
    var version: Int
    var sort: [Double]
    var found: Bool
    var seqNo, primaryTerm: Int
    var seqId, sessionId, from, to: String
    var messageType: Int
    var text, ext: String
    var timestamp: Int64
    var recallTime: Int64?
    var recall: Bool?
    var atList: [String]?
    var increment: Int
    var discussSearchText, uniqueIdentification: String?
    var currUserRead: Bool
    var unreadNum, messageReceiveNum, messageReviewNum: Int?
    var currUserReview: Bool
    
    //Transient
    var highlight: Bool? = false

    enum CodingKeys: String, CodingKey {
        case id, version, sort, found, seqNo, primaryTerm
        case seqId
        case sessionId
        case from, to, messageType, text, ext, timestamp, recall, recallTime, atList, increment, discussSearchText, uniqueIdentification, currUserRead, unreadNum, messageReceiveNum, messageReviewNum, currUserReview
    }

    init(id: String, version: Int, sort: [Double], found: Bool, seqNo: Int, primaryTerm: Int, seqId: String, sessionId: String, from: String, to: String, messageType: Int, text: String, ext: String, timestamp: Int64, recall: Bool?, recallTime: Int64?, atList: [String]?, increment: Int, discussSearchText: String?, uniqueIdentification: String?, currUserRead: Bool, unreadNum: Int?, messageReceiveNum: Int?, messageReviewNum: Int?, currUserReview: Bool) {
        self.id = id
        self.version = version
        self.sort = sort
        self.found = found
        self.seqNo = seqNo
        self.primaryTerm = primaryTerm
        self.seqId = seqId
        self.sessionId = sessionId
        self.from = from
        self.to = to
        self.messageType = messageType
        self.text = text
        self.ext = ext
        self.timestamp = timestamp
        self.recall = recall
        self.recallTime = recallTime
        self.atList = atList
        self.increment = increment
        self.discussSearchText = discussSearchText
        self.uniqueIdentification = uniqueIdentification
        self.currUserRead = currUserRead
        self.unreadNum = unreadNum
        self.messageReceiveNum = messageReceiveNum
        self.messageReviewNum = messageReviewNum
        self.currUserReview = currUserReview
    }
}

// MARK: ChatMessageBean convenience initializers and mutators

extension ChatMessageBean {
    convenience init(data: Data) throws {
        let me = try newJSONDecoder().decode(ChatMessageBean.self, from: data)
        self.init(id: me.id, version: me.version, sort: me.sort, found: me.found, seqNo: me.seqNo, primaryTerm: me.primaryTerm, seqId: me.seqId, sessionId: me.sessionId, from: me.from, to: me.to, messageType: me.messageType, text: me.text, ext: me.ext, timestamp: me.timestamp, recall: me.recall, recallTime: me.recallTime, atList: me.atList, increment: me.increment, discussSearchText: me.discussSearchText, uniqueIdentification: me.uniqueIdentification, currUserRead: me.currUserRead, unreadNum: me.unreadNum, messageReceiveNum: me.messageReceiveNum, messageReviewNum: me.messageReviewNum, currUserReview: me.currUserReview)
    }

    convenience init(_ json: String, using encoding: String.Encoding = .utf8) throws {
        guard let data = json.data(using: encoding) else {
            throw NSError(domain: "JSONDecoding", code: 0, userInfo: nil)
        }
        try self.init(data: data)
    }

    convenience init(fromURL url: URL) throws {
        try self.init(data: try Data(contentsOf: url))
    }

    func with(
        id: String? = nil,
        version: Int? = nil,
        sort: [Double]? = nil,
        found: Bool? = nil,
        seqNo: Int? = nil,
        primaryTerm: Int? = nil,
        seqId: String? = nil,
        sessionId: String? = nil,
        from: String? = nil,
        to: String? = nil,
        messageType: Int? = nil,
        text: String? = nil,
        ext: String? = nil,
        timestamp: Int64? = nil,
        recall: Bool?? = nil,
        recallTime: Int64?? = nil,
        atList: [String]?? = nil,
        increment: Int? = nil,
        discussSearchText: String?? = nil,
        uniqueIdentification: String?? = nil,
        currUserRead: Bool? = nil,
        unreadNum: Int?? = nil,
        messageReceiveNum: Int?? = nil,
        messageReviewNum: Int?? = nil,
        currUserReview: Bool? = nil
    ) -> ChatMessageBean {
        return ChatMessageBean(
            id: id ?? self.id,
            version: version ?? self.version,
            sort: sort ?? self.sort,
            found: found ?? self.found,
            seqNo: seqNo ?? self.seqNo,
            primaryTerm: primaryTerm ?? self.primaryTerm,
            seqId: seqId ?? self.seqId,
            sessionId: sessionId ?? self.sessionId,
            from: from ?? self.from,
            to: to ?? self.to,
            messageType: messageType ?? self.messageType,
            text: text ?? self.text,
            ext: ext ?? self.ext,
            timestamp: timestamp ?? self.timestamp,
            recall: recall ?? self.recall,
            recallTime: recallTime ?? self.recallTime,
            atList: atList ?? self.atList,
            increment: increment ?? self.increment,
            discussSearchText: discussSearchText ?? self.discussSearchText,
            uniqueIdentification: uniqueIdentification ?? self.uniqueIdentification,
            currUserRead: currUserRead ?? self.currUserRead,
            unreadNum: unreadNum ?? self.unreadNum,
            messageReceiveNum: messageReceiveNum ?? self.messageReceiveNum,
            messageReviewNum: messageReviewNum ?? self.messageReviewNum,
            currUserReview: currUserReview ?? self.currUserReview
        )
    }

    func jsonData() throws -> Data {
        return try newJSONEncoder().encode(self)
    }

    func jsonString(encoding: String.Encoding = .utf8) throws -> String? {
        return String(data: try self.jsonData(), encoding: encoding)
    }
}


enum ChatMessageType : Int {
    case Text = 0
    
    
}
